import 'package:flutter/material.dart';

import '../../services/screen_adapter.dart';
import '../../model/product_content.dart';


class CartNum extends StatefulWidget {

  final ProductContentItem _productContent;

  CartNum(this._productContent, {Key key}) : super(key: key);

  @override
  _CartItemState createState() => _CartItemState();
}

class _CartItemState extends State<CartNum> {

  ProductContentItem _productContent;

  @override
  void initState() {
    super.initState();
    _productContent = widget._productContent;
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init();
    return Container(
      width: ScreenAdapter.width(168),
      decoration: BoxDecoration(
        border: Border.all(
          width: ScreenAdapter.width(2),
          color: Colors.black12
        )
      ),
      child: Row(
        children: <Widget>[
          _leftBtn(),
          _centerArea(),
          _rightBtn()
        ]
      )
    );
  }

  /// 左侧按钮
  Widget _leftBtn() {
    return InkWell(
      onTap: (){
        if( _productContent.count >1) {
          setState(() => _productContent.count -= 1);
        } 
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenAdapter.width(45),
        height: ScreenAdapter.height(45),
        child: Text('-')
      )
    );
  }

  /// 右侧按钮
  Widget _rightBtn() {
    return InkWell(
      onTap: (){
        setState(() => _productContent.count += 1);
      },
      child: Container(
        alignment: Alignment.center,
        width: ScreenAdapter.width(45),
        height: ScreenAdapter.height(45),
        child: Text('+')
      )
    );
  }

  /// 中间区域
  Widget _centerArea() {
    return Container(
      alignment: Alignment.center,
      width: ScreenAdapter.width(70),
      height: ScreenAdapter.height(45),
      decoration: BoxDecoration(
        border: Border(
          left: BorderSide(
            width: ScreenAdapter.width(2),
            color: Colors.black12
          ),
          right: BorderSide(
            width: ScreenAdapter.width(2),
            color: Colors.black12
          )
        )
      ),
      child: Text('${_productContent.count}')
    );
  }
}